<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        /* 法一： */
        .box{
            width:100px ;
            height: 100px;
            margin: 100px auto;
            background-color: pink;
            font: bolder 25px/100px "宋体";
            text-align: center;
            
        }
        /* 法二： */
        .box2{
            position: relative;
            left: 50%;
            width:100px ;
            height: 100px;
            margin-left: -50px;
            background-color: yellow;
            font: bolder 25px/100px "宋体";
            text-align: center;
            
        }
        /* 法三： */
        .box3{
            position: relative;
            left: 50%;
            width: 100px;
            height: 100px;
            transform: translate(-50%);
            /* transform: translateX(-50%); */

            background-color: green;
            font: bolder 25px/100px "宋体";
            text-align: center;
        }

        
    </style>
</head>
<body>
    <div class="box">
        法一
    </div>
    <div class="box2">法二</div>
    <div class="box3">法三</div>
</body>
</html>